<?php
/**
 * Created by .
 * User: luogan
 * Date: 2022/1/17
 */

namespace app\admin\validate;


use app\admin\controller\v1\Menu;
use app\model\MenuModel;

class MenuValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require|isPositiveInteger',
        'title' => 'require|max:25',
        'icon' => 'require|max:255',
        'type' => 'require|between:0,2',
        'parent_id' => 'require|isInteger',
        'component' => 'require|max:255',
        'path' => 'require|max:255',
        'action' => 'require|max:255',
        'module' => 'max:255',
        'controller' => 'max:255',
        'method' => 'max:255',
        'sort' => 'require|isInteger',
        'target' => 'max:255',
        'status' => 'require|between:0,1',
        'create_time' => 'dateFormat:Y-m-d H:i:s',
        'page' => 'isPositiveInteger',
        'size' => 'isPositiveInteger',
    ];

    protected $message = [
    ];

    protected $scene = [
        'list' => ['page','size'],
        'info' => ['id'],
        'add' => ['title', 'icon','type', 'parent_id', 'component','path', 'action','module', 'controller', 'method','sort','target', 'status'],
        'edit' => ['id', 'title', 'icon','type', 'parent_id', 'component','path', 'action','module', 'controller', 'method','sort','target', 'status'],
        'del' => ['id'],
    ];

    public function sceneDel()
    {
        return $this->only($this->scene[$this->currentScene])->append('id', 'isMenuChildren');
    }

    /**
     * 验证菜单是否有子类
     * @param $value
     * @param string $rule
     * @param string $data
     * @param string $field
     * @return bool|string
     */
    protected function isMenuChildren($value, $rule='', $data='', $field='')
    {
        $result = MenuModel::where('parent_id',$value)->count();

        if ($result>0) {
            return $field . '有子类菜单,不能删除该菜单';
        }

        return true;

    }
}
